﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Reflection;
using System.Text;

using NUnit.Framework;

namespace Refix.Core.Tests.Commands
{
    [TestFixture]
    public class AlternativeTest : TestSolutionFolderTest
    {
        [Test]
        public void Help_Shows_Appropriate_Text()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("help");
            queue.Enqueue("alt");
            new CommandWithOptions(queue).Command.Execute();
            StringAssert.Contains(": rfx alt ", builder.ToString(), "Alternative help text didn't include short form.");
            StringAssert.Contains(": rfx alternative ", builder.ToString(),
                                  "Alternative help text didn't include long form.");
        }

        [Test]
        public void Validate_Fails_With_Both_Verbose_And_Quiet()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("alt");
            queue.Enqueue("-v");
            queue.Enqueue("-q");
            queue.Enqueue("assembly");
            queue.Enqueue("version1");
            queue.Enqueue("version2");
            var options = new CommandWithOptions(queue);
            Assert.False(options.Command.Validate());
            Assert.IsNotEmpty(builder.ToString(),
                              "Alternative command didn't report problem with conflicting options.");
        }

        [Test]
        public void Validate_Fails_With_Wrong_Number_Of_Options()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("alt");
            queue.Enqueue("-v");
            queue.Enqueue("assembly");
            queue.Enqueue("version1");
            var options = new CommandWithOptions(queue);
            Assert.False(options.Command.Validate());
            Assert.IsNotEmpty(builder.ToString(),
                              "Alternative command didn't report problem with wrong number of arguments.");
        }

        [Test]
        public void Validate_Passes_With_Full_Set_Of_Arguments()
        {
            var queue = new Queue<string>();
            queue.Enqueue("alt");
            queue.Enqueue("-q");
            queue.Enqueue("assembly");
            queue.Enqueue("version1");
            queue.Enqueue("version2");
            var options = new CommandWithOptions(queue);
            Assert.True(options.Command.Validate());
        }

        [Test]
        public void Add_Shows_Warning_With_Preexisting_Alternative()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("alt");
            queue.Enqueue("Refix.Test.B");
            queue.Enqueue("0.1.0.2");
            queue.Enqueue("0.1.0.2+");
            var options = new CommandWithOptions(queue);
            Assert.True(options.Command.Execute(), "Alternative command should have succeeded if alternative already set.");
            StringAssert.Contains("already", builder.ToString(),
                              "Alternative command didn't report problem with preexisting alternative.");
        }

        [Test]
        public void Delete_Shows_Error_With_Missing_Alternative()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("alt");
            queue.Enqueue("-d");
            queue.Enqueue("Refix.Test.B");
            queue.Enqueue("0.1.0.1");
            queue.Enqueue("0.1.0.2");
            var options = new CommandWithOptions(queue);
            Assert.False(options.Command.Execute(), "Alternative command should have failed deleting non-existent alternative.");
            StringAssert.Contains("not set", builder.ToString(),
                              "Alternative command didn't report problem with preexisting alternative.");
        }

        [Test]
        public void Errors_With_Unknown_Component()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("alt");
            queue.Enqueue("-f");
            queue.Enqueue("Not.Found");
            queue.Enqueue("0.1.0.1");
            queue.Enqueue("0.1.0.2");
            var options = new CommandWithOptions(queue);
            Assert.False(options.Command.Execute(), "Alternative command should have failed with unknown component.");
            StringAssert.Contains("unknown assembly", builder.ToString(),
                              "Alternative command didn't report problem with unknown assembly.");
        }

        [Test]
        public void Errors_With_Unknown_Version()
        {
            var builder = new StringBuilder();
            OutputWriter.Writer = new StringWriter(builder);
            var queue = new Queue<string>();
            queue.Enqueue("alt");
            queue.Enqueue("Refix.Test.B");
            queue.Enqueue("0.1.0.11");
            queue.Enqueue("0.1.0.12");
            var options = new CommandWithOptions(queue);
            Assert.False(options.Command.Execute(), "Alternative command should have failed with unknown version.");
            StringAssert.Contains("unknown version", builder.ToString(),
                              "Alternative command didn't report problem with unknown version.");
        }
    }
}